package com.zelin.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.zelin.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.zelin.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.zelin.gulimall.product.entity.AttrEntity;
import com.zelin.gulimall.product.service.AttrService;
import com.zelin.gulimall.product.vo.AttrAttrGroupVo;
import com.zelin.gulimall.product.vo.AttrGroupAttrVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zelin.common.utils.PageUtils;
import com.zelin.common.utils.Query;

import com.zelin.gulimall.product.dao.AttrGroupDao;
import com.zelin.gulimall.product.entity.AttrGroupEntity;
import com.zelin.gulimall.product.service.AttrGroupService;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    private AttrAttrgroupRelationDao relationDao;
    @Autowired
    private AttrService attrService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, long catelogId) {
        IPage<AttrGroupEntity> page = new Query<AttrGroupEntity>().getPage(params);
        //1.首先，按照分类查询
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<AttrGroupEntity>();
        //2.如果存在关键字则按照关键字查询
        String key = (String) params.get("key");
        if(StringUtils.isNotBlank(key)){
            wrapper.and(
                    cond->cond.eq("attr_group_id",key).or().like("attr_group_name",key)
                    );
        }
        if(catelogId != 0){
            wrapper.eq("catelog_id",catelogId);
        }
        IPage<AttrGroupEntity> iPage = this.page(page, wrapper);

        return new PageUtils(iPage);
    }

    //根据分组id查询分组及其属性列表
    @Override
    public List<AttrGroupAttrVo> findAttrGroupAttrByCategoryId(Long catelogId) {
        //方法一：伟统方法
        //List<AttrGroupAttrVo> vos = new ArrayList<>();
        //1.在分组表中根据当前分类id查询分组id
        // List<AttrGroupEntity> groupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        // for (AttrGroupEntity groupEntity : groupEntities) {
        //     List<AttrEntity> entityList = attrService.relationAttr(groupEntity.getAttrGroupId());
        //     AttrGroupAttrVo vo = new AttrGroupAttrVo();
        //     BeanUtils.copyProperties(groupEntities,vo);
        //     vo.setAttrs(entityList);
        //     vos.add(vo);
        // }
        //return vos;

        //方法二：lamba表达式 +　stream编程
        List<AttrGroupEntity> groupEntities = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        List<AttrGroupAttrVo> collect = groupEntities.stream().map(m -> {
            AttrGroupAttrVo vo = new AttrGroupAttrVo();
            BeanUtils.copyProperties(m, vo);
            List<AttrEntity> entityList = attrService.relationAttr(m.getAttrGroupId());
            vo.setAttrs(entityList);
            return vo;
        }).collect(Collectors.toList());
        return collect;
    }


}